package com.coffee.admin.controller.supplier;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.supplier.query.SupplierBaseDeployQuery;
import com.coffee.admin.api.supplier.result.SupplierBaseDeployResult;
import com.coffee.admin.api.supplier.result.SupplierResult;
import com.coffee.admin.api.supplier.service.SupplierBaseDeployService;
import com.coffee.admin.api.supplier.service.SupplierService;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.SupplierLoginCheck;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.enums.SkuTypeEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.sku.api.inquire.rep.InquireSkuListRep;
import com.coffee.sku.api.inquire.req.InquireSkuAddReq;
import com.coffee.sku.api.inquire.req.InquireSkuUpdateReq;
import com.coffee.sku.api.inquire.result.InquireSkuContentResult;
import com.coffee.sku.api.inquire.service.InquireSkuContentService;
import com.coffee.sku.api.sku.req.SkuListReq;
import com.coffee.sku.api.sku.result.SkuCategoryResult;
import com.coffee.sku.api.sku.service.SkuCategoryService;
import com.coffee.sku.api.sku.service.SkuContentService;
import com.coffee.sku.api.sku.service.SkuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/supplier/inquire")
public class SupplierInquireSkuController extends BaseController {

    @DubboReference(check = false)
    private InquireSkuContentService inquireSkuContentService;
    @DubboReference(check = false)
    private SkuCategoryService skuCategoryService;
    @DubboReference(check = false)
    private SupplierBaseDeployService supplierBaseDeployService;
    @DubboReference(check = false)
    private SkuContentService skuContentService;
    @DubboReference(check = false)
    private SupplierService supplierService;

    @OperLog(operModul = "供应商-商品管理-新增商品", operDesc = "供应商-新增商品")
    @SupplierLoginCheck(alias = "SupplierInquireSkuOperationAlias")
    @RequestMapping(value = "/sku/add", method = {RequestMethod.POST})
    public Result<String> add(HttpServletRequest request, @RequestBody InquireSkuAddReq skuAddReq) {
        SupplierResult supplierResult = supplierService.getDetail(getSupplier().getSupplierId());
        if (!supplierResult.getSalePermission().contains(SkuTypeEnum.INQUIRE.getCode())){
            return Result.failed(ExceptionEnum.ERROR_200014);
        }
        Result<String> result = Result.success(null);
        try {
            skuAddReq.setSupplierId(getSupplier().getSupplierId());
            inquireSkuContentService.addInquireSku(skuAddReq);
            result.setData("SUCCESS");
            return result;
        } catch (BusinessException e) {
            Result<String> failed = Result.failed(e.getCode(), e.getMessage());
            return failed;
        }
    }


    @OperLog(operModul = "供应商-商品管理-修改商品", operDesc = "修改商品")
    @SupplierLoginCheck(alias = "SupplierInquireSkuOperationAlias")
    @RequestMapping(value = "/sku/update", method = {RequestMethod.POST})
    public Result<String> updateSku(HttpServletRequest request, @RequestBody InquireSkuUpdateReq skuUpdateReq) {
        SupplierResult supplierResult = supplierService.getDetail(getSupplier().getSupplierId());
        if (!supplierResult.getSalePermission().contains(SkuTypeEnum.INQUIRE.getCode())){
            return Result.failed(ExceptionEnum.ERROR_200014);
        }
        Result<String> result = Result.success(null);
        try {
            inquireSkuContentService.updateInquireSku(skuUpdateReq);
            result.setData("SUCCESS");
            return result;
        } catch (BusinessException e) {
            Result<String> failed = Result.failed(e.getCode(), e.getMessage());
            return failed;
        }
    }


    @OperLog(operModul = "供应商-商品管理-修改商品", operDesc = "修改商品")
    @SupplierLoginCheck(alias = "SupplierInquireSkuOperationAlias")
    @RequestMapping(value = "/sku/updateIsEnable", method = {RequestMethod.POST})
    public Result<String> updateIsEnable(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("skuId");
        String isEnable = jsonObject.getString("isEnable");
        Result<String> result = Result.success(null);
        try {
            skuContentService.updateIsEnable(id,isEnable, SkuTypeEnum.INQUIRE.getCode(), getSupplier().getSupplierId());
            result.setData("SUCCESS");
            return result;
        } catch (BusinessException e) {
            Result<String> failed = Result.failed(e.getCode(), e.getMessage());
            return failed;
        }
    }

    @OperLog(operModul = "供应商-商品管理-删除商品", operDesc = "删除商品")
    @SupplierLoginCheck(alias = "SupplierInquireSkuOperationAlias")
    @RequestMapping(value = "/sku/delete", method = {RequestMethod.POST})
    public Result<String> deleteSku(@RequestBody JSONObject json, HttpServletRequest request) {
        SupplierResult supplierResult = supplierService.getDetail(getSupplier().getSupplierId());
        if (!supplierResult.getSalePermission().contains(SkuTypeEnum.INQUIRE.getCode())){
            return Result.failed(ExceptionEnum.ERROR_200014);
        }
        Result<String> result = Result.success(null);
        try {
            Integer skuId = json.getInteger("skuId");
            inquireSkuContentService.delete(skuId);
            result.setData("SUCCESS");
            return result;
        } catch (BusinessException e) {
            Result<String> failed = Result.failed(e.getCode(), e.getMessage());
            return failed;
        }
    }


    @OperLog(operModul = "平台-商品管理-期货商品", operDesc = "分页查询期货商品列表")
    @SupplierLoginCheck(alias = "SupplierInquireSkuListAlias")
    @RequestMapping(value = "/sku/findList", method = {RequestMethod.POST})
    public Result<Page<InquireSkuListRep>> inquireFindList(HttpServletRequest request, @RequestBody SkuListReq params) {
        SupplierResult supplierResult = supplierService.getDetail(getSupplier().getSupplierId());
        if (!supplierResult.getSalePermission().contains(SkuTypeEnum.INQUIRE.getCode())){
            return Result.failed(ExceptionEnum.ERROR_200014);
        }
        LoginUser supplier = getSupplier();
        params.setSupplierId(supplier.getSupplierId());
        Result<Page<InquireSkuListRep>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        Integer status = params.getStatus();
        if (status != null && status == 0) {
            params.setIsApprove("W");
        } else if (status != null && status == 1) {
            params.setIsApprove("N");
        } else if (status != null && status == 2) {
            params.setIsApprove("Y");
        } else if (status != null && status == 3) {
            params.setIsEnable("N");
        } else if (status != null && status == 4) {
            params.setIsEnable("Y");
        }
        result.setData(inquireSkuContentService.findList(params, pagerInfo));
        return result;
    }

    @OperLog(operModul = "平台-期货商品管理-商品详情", operDesc = "期货商品详情")
    @SupplierLoginCheck(alias = "SupplierInquireSkuListAlias")
    @RequestMapping(value = "/sku/detail")
    public Result<InquireSkuContentResult> inquireDetail(HttpServletRequest request, @RequestBody JSONObject jsonObject) throws BusinessException {
        SupplierResult supplierResult = supplierService.getDetail(getSupplier().getSupplierId());
        if (!supplierResult.getSalePermission().contains(SkuTypeEnum.INQUIRE.getCode())){
            return Result.failed(ExceptionEnum.ERROR_200014);
        }
        Integer skuId = jsonObject.getInteger("skuId");
        Result<InquireSkuContentResult> result = Result.success(null);
        InquireSkuContentResult detail = inquireSkuContentService.getDetail(skuId, getSupplier().getSupplierId());
        result.setData(detail);
        return result;
    }

    @OperLog(operModul = "供应商-类目管理", operDesc = "查看类目列表")
    @PostMapping(value = "/category/all/list")
    public Result<List<SkuCategoryResult>> skuCategoryAllList(HttpServletRequest request, HttpServletResponse response) {
        List<SkuCategoryResult> list = skuCategoryService.getList("N");
        return Result.success(list);
    }


}
